package com.gjt.dlmall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.gjt.common.vaild.AddGroup;
import com.gjt.common.vaild.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gjt.dlmall.product.entity.BrandEntity;
import com.gjt.dlmall.product.service.BrandService;
import com.gjt.common.utils.PageUtils;
import com.gjt.common.utils.R;



/**
 * 品牌表
 *
 * @author gjt
 * @email g45852139@163.com
 * @date 2021-09-02 23:10:21
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("id") Integer id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(value = {AddGroup.class}) @RequestBody BrandEntity brand){
        //使用注解@Valid , BindingResult result
        //声明一个Map进行接收
       /* Map<String,String> map = new HashMap<>();
        if (result.hasErrors()) {
            //1、获取效验的错误结果
            result.getFieldErrors().forEach((fieldError)->{
                //获取每一个错误的属性名称
                String field = fieldError.getField();
                //获取每个错误提示
                String message = fieldError.getDefaultMessage();
                map.put(field,message);
            });
            return R.error(400,"提交的数据不合法").put("data","数据不合法");
        }else {
            brandService.save(brand);
            return R.ok();
        }*/
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Integer[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
